package cn.myfirefly.moming.portal.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.myfirefly.moming.portal.dao.mapper.MmExportConfigMapper;
import cn.myfirefly.moming.portal.entity.MmExportConfig;
import cn.myfirefly.moming.portal.model.MmExportConfigDto;
import cn.myfirefly.moming.portal.model.MmExportConfigFilterDto;
import cn.myfirefly.core.BaseDao;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.mybatis.BjuiDataGridFilter;
import cn.myfirefly.core.mybatis.BjuiDataGridSqlHelper;

@Repository
public class MmExportConfigDao extends BaseDao<MmExportConfig> {
	
	@Autowired
	private MmExportConfigMapper exportConfigMapper;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PageResultDto<MmExportConfigDto> getList(MmExportConfigFilterDto dto) {
//		Session session = sessionFactory.getCurrentSession();
//		PageResultDto resultDto = new PageResultDto();
//		
//		StringBuffer countSql = new StringBuffer("select count(r) from MmExportConfig r where 1=1 ");
//		StringBuffer querySql = new StringBuffer("from MmExportConfig r where 1=1 ");
//		
//		List paramList = new ArrayList();
//		FilterUtil.handleFilterSql(dto, "r", countSql, querySql, paramList);
//		
//		Query query = session.createQuery(countSql.toString());
//		FilterUtil.setQueryParams(query, paramList);
//		Number number = (Number)query.uniqueResult();
//		resultDto.setTotal(number.intValue());
//		if (number.intValue() > 0) {
//			query = session.createQuery(querySql.toString());
//			FilterUtil.setQueryParams(query, paramList);
//			List<MmExportConfig> list = query
//					.setFirstResult((dto.getPageCurrent() - 1) * dto.getPageSize())
//					.setMaxResults(dto.getPageSize())
//					.list();
//			resultDto.setPageCurrent(dto.getPageCurrent());
//			resultDto.setList(list);
//		}
		PageResultDto resultDto = new PageResultDto();
		BjuiDataGridFilter bjuiDataGridFilter = BjuiDataGridSqlHelper.addFilter(dto);
		Page<MmExportConfigDto> page = PageHelper.startPage(dto.getPageCurrent(), dto.getPageSize(), true);
		List<MmExportConfigDto> list = exportConfigMapper.getList(bjuiDataGridFilter.getParam());
		resultDto.setPageCurrent(dto.getPageCurrent());
		resultDto.setTotal((int)page.getTotal());
		resultDto.setList(list);
		return resultDto;
	}
	
}
